package com.hw.one.collect.dust.job;

import com.hw.one.collect.client.ClientFactory;
import com.hw.one.collect.dust.redis.BaseRedis;
import com.hw.one.collect.dust.util.WeatherUtil;
import com.hw.one.collect.dust.constans.SjwConstants;
import com.hw.one.collect.dust.dao.RaiseDustDao;
import com.hw.one.collect.dust.po.RaiseDust;
import com.hw.one.collect.dust.po.SiteInfo;
import com.hw.one.collect.mapper.SiteDao;
import com.hw.one.collect.spadata.po.MissData;
import com.hw.one.collect.util.DateUtil;
import com.hw.one.collect.util.MathCalculateUtil;
import com.hw.one.collect.util.SerializeUtil;
import com.mlb.analyse.webservice.DmDataInfoVO;
import com.mlb.analyse.webservice.GetRealTimePmData;
import org.apache.cxf.endpoint.Client;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/05/17 14:37.
 *
 * 市建委数据拉取任务
 */
@Component
public class RaiseDustDataJob extends RaiseDustBaseJob{

    private static Logger log = Logger.getLogger(RaiseDustDataJob.class);

    private static DecimalFormat df = new DecimalFormat("#####.####");
    private static DecimalFormat df_0 = new DecimalFormat("#####");
    @Resource
    private SiteDao siteDao;

    @Resource
    private RaiseDustDao raiseDustDao;

    public void start() {
        SjwConstants.getRegionIdWithName();
        Map<String, String> regionIdWithOtherCode = SjwConstants.regionIdWithOtherCode;
        Map<String, String> regionIdWithName = SjwConstants.regionIdWithName;
        log.info("-----------------------------请求链接---------------------");
        log.info(SjwConstants.Monitor_URI);
        log.info("----------------------------------------------------------");
        List<RaiseDust> raiseDustList = new ArrayList<>();

        for (String key : regionIdWithOtherCode.keySet()) {
            try {
                log.info("-------------------当前请求的城市：" + regionIdWithName.get(key) + "-----------");
                // 构建查询参数
                GetRealTimePmData realTimePmData = new GetRealTimePmData();
                realTimePmData.setDistrictCode(regionIdWithOtherCode.get(key));
                // 发送请求处理数据,传入方法名和参数类
                List<RaiseDust> raiseDustList2 = executeGetRealTimeData(SjwConstants.getRealData, realTimePmData);
                raiseDustList.addAll(raiseDustList2);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        saveData(raiseDustList);
    }


    /**
     * 判断并保存数据
     * @author 李亚坤
     * @createtime 2017年8月23日18:22:34
     * @param raiseDustList
     */
    public void saveData(List<RaiseDust> raiseDustList) {

        // 要更新的数据
        List<RaiseDust> updateRaiseDustList = new ArrayList<>();
        // 要保存的数据
        List<RaiseDust> insertRaiseDustList = new ArrayList<>();
        List<String> siteCodeTo = new ArrayList<>();
        String monitorTime = null;
        // 循环数据，处理
        for (RaiseDust raiseDust : raiseDustList) {
            String siteCode = raiseDust.getSITECODE();
            siteCodeTo.add(siteCode);

            monitorTime = raiseDust.getMONITORTIME2();
            String siteName = raiseDust.getSITENAME();
            // 先判断数据是否跟上次的数据一致
            RaiseDust lastRaiseDust = baseInfoService.getLastDataBySiteCode(siteCode);
            if (null == lastRaiseDust || DateUtil.compare(raiseDust.getMONITORTIME(), lastRaiseDust.getMONITORTIME())) {
                // 判断数据是否跟上次数据一致
                updateRaiseDustList.add(raiseDust);
                // 此次数据跟上次数据不一致，保存到数据库
                insertRaiseDustList.add(raiseDust);
                baseInfoService.insertLastData(raiseDust.getSITECODE(), raiseDust);
            }
            if (!baseInfoService.getUseSiteCodeList().contains(siteCode)) {
                // 未更新，要更新,只更新状态和时间
                SiteInfo siteInfo = new SiteInfo();
                siteInfo.setUpdateDate(new Date());
                siteInfo.setIsUse("1");
                siteInfo.setSiteName(raiseDust.getSITENAME());
                siteInfo.setSiteCode2(raiseDust.getSITECODE2());
                siteDao.updateSiteBySiteName(siteInfo);
                baseInfoService.addUseSiteCode(siteCode);
            }
        }
        raiseDustDao.updateRealRaiseDust(updateRaiseDustList);
        log.debug("--------------本次市建委扬尘数据保存：" + insertRaiseDustList.size() + " 个");
        raiseDustDao.saveRaiseDustDataList(insertRaiseDustList);

        // 需要查询的总数据
//        List<SiteInfo> siteInfoLists = siteDao.selectSiteInfoBySiteType("106");
        List<String> useSiteCodeList = baseInfoService.getUseSiteCodeList();
        for (String useSiteCode : useSiteCodeList) {
            if (!siteCodeTo.contains(useSiteCode)) {
//                baseRedis.addList("sjwRaiseDustMissData", siteInfo.getSiteCode() + "|" + thisTime + "|0");
                MissData missData = new MissData();
                missData.setCount(0);
                missData.setMONITORTIME(monitorTime);
                missData.setSITECODE(useSiteCode);
                baseInfoService.addMissData(SerializeUtil.ObjTOSerialize(missData));
            }
        }
    }


    public boolean checkDataSame(RaiseDust raiseDust1, RaiseDust raiseDust2) {
        if (raiseDust1 == null || raiseDust2 == null) {
            return false;
        }
        // 如果新的 时间大于上次时间
        if (DateUtil.compare(raiseDust1.getMONITORTIME(), raiseDust2.getMONITORTIME())) {
            return false;
        }
        // 如果两个对象一致的话，返回true
        return raiseDust1.getMONITORTIME2().equals(raiseDust2.getMONITORTIME2())
                && raiseDust1.getPM10().equals(raiseDust2.getPM10())
                && raiseDust1.getPM25().equals(raiseDust2.getPM25())
                && raiseDust1.getNOISE().equals(raiseDust2.getNOISE())
                && raiseDust1.getTEMP().equals(raiseDust2.getTEMP())
                && raiseDust1.getHUM().equals(raiseDust2.getHUM())
                && raiseDust1.getWD().equals(raiseDust2.getWD())
                && raiseDust1.getWS().equals(raiseDust2.getWS());
    }
}
